﻿using Newtonsoft.Json;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace WpfApp1.Services
{
    public class HttpRestClient
    {
        private readonly string apiUrl;
        protected readonly RestClient client;

        public HttpRestClient(string apiUrl = "http://120.26.132.73:9090")
        {
            this.apiUrl = apiUrl;
            client = new RestClient();
        }

    
      
        public async Task<ApiResponse<T>> ExecuteAsync<T>(BaseRequest baseRequest)
        {
            var uri = new Uri(apiUrl + baseRequest.Route);
            var request = new RestRequest(uri, baseRequest.Method);
            request.AddHeader("Content-Type", baseRequest.ContentType);
            if (baseRequest.Parameter != null)
            {
                request.AddJsonBody(baseRequest.Parameter);
            }

            var response = await client.ExecuteAsync(request);
            if (response.StatusCode == System.Net.HttpStatusCode.OK)
                return JsonConvert.DeserializeObject<ApiResponse<T>>(response.Content);

            else
                return new ApiResponse<T>()
                {
                    Code = 500,
                    Msg ="服务器错误"
                };
        }
        public async Task<ApiResponse<T>> ExecuteAsyncParms<T>(BaseRequest baseRequest, IEnumerable<Parameter>? parameters = null, string? json = null)
        {
            var uri = new Uri(apiUrl + baseRequest.Route);
            var request = new RestRequest(uri, baseRequest.Method);
            request.AddHeader("Content-Type", baseRequest.ContentType);
            if (parameters != null)
            {

                foreach (var param in parameters)
                {
                    request.AddParameter(param);
                }
            }
            if (json != null)
            {

                request.AddJsonBody(json);
            }
            var response = await client.ExecuteAsync(request);
            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                var result = JsonConvert.DeserializeObject<ApiResponse<T>>(response.Content);
                if (result.Code != 200)
                {
                    return new ApiResponse<T>()
                    {
                        Code = 500,
                        Msg = result.Msg
                    };
                }
                return result;
            }

            else
                return new ApiResponse<T>()
                {
                    Code = 500,
                    Msg = "服务器错误"
                };
        }
    }
    public class BaseRequest
    {
        public Method Method { get; set; }
        public string Route { get; set; }
        public string ContentType { get; set; } = "application/json";
        public object Parameter { get; set; }
    }
    public class ApiResponse
    {
        public string Msg { get; set; }

        public int Code { get; set; }

        public object Data { get; set; }
    }

    public class ApiResponse<T>
    {
        public string Msg { get; set; }

        public int Code { get; set; }

        public T Data { get; set; }
    }
}
